package com.returnsoft.resource.service;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.InterviewDto;
import com.returnsoft.resource.eao.InterviewEao;
import com.returnsoft.resource.entity.Interview;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class InterviewBean
 */
@Stateless
@LocalBean
public class InterviewBean implements InterviewInterface {

	@EJB
	private InterviewEao eao;

	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public InterviewBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<InterviewDto> findList(List<Integer> areasId,
			List<Integer> subAreasId,String interviewedAt,
			Integer stateId, Integer recruiterId,
			String candidateDocumentNumber, String candidateFirstname,
			String candidateLastname) throws RecruitmentException {

		try {

			List<InterviewDto> entities = eao.findList(areasId, subAreasId, interviewedAt,
					stateId, recruiterId, candidateDocumentNumber,
					candidateFirstname, candidateLastname);

			return entities;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}

	}

	@Override
	public InterviewDto add(InterviewDto dto) throws RecruitmentException {
		try {
			
			

			Interview entity = conv.toInterview(dto);
			entity = eao.add(entity);
			dto = conv.fromInterview(entity);

			return dto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public InterviewDto findById(Integer id) throws RecruitmentException {
		try {
			Interview entity = eao.findById(id);
			InterviewDto dto = conv.fromInterview(entity);
			return dto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	/*@Override
	public List<InterviewDto> findByDocumentNumber(String documentNumber)
			throws RecruitmentException {
		try {
			List<InterviewDto> interviews = eao
					.findByDocumentNumber(documentNumber);

			return interviews;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}*/
	
	@Override
	public List<InterviewDto> findPendingsByCandidateId(Integer candidateId)
			throws RecruitmentException {
		try {
			List<InterviewDto> interviews = eao
					.findPendingsByCandidateId(candidateId);

			return interviews;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
